<?php
namespace app\controller;

use app\controller\BaseController;
use model\CampQuery;
use model\PaysQuery;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class FrontController extends BaseController
{
    public function indexAction(Request $request)
    {
        $pays = PaysQuery::create()->joinCamp('Camp', \Propel\Runtime\ActiveQuery\Criteria::INNER_JOIN)->find();
        $camps = CampQuery::create()->find();
        $refugies = \model\RefugieQuery::create()->find();
        $nomrefugies = array();
        foreach ($refugies as $value) {
            $nomRefugies[]= $value->getNom(); 
        }
        
        return $this->render('index.php', 
                array('pays' => $pays,'camps'=>$camps,'noms' => json_encode($nomRefugies)));
    }
    
    public function ajaxInformationFromPaysAction(Request $request)
    {
        $session = $this->getSession();
        
        if($request->isXmlHttpRequest()) 
        {
            
            $form = $request->request->all();
            
            $nomPays = $form['nomPays'];
            
            $pays = PaysQuery::create()->findOneByNom($nomPays);
            $camps = CampQuery::create()->filterByPays($pays)->find();
            
            $response['valid'] = true;
            
            $result= $this->render('pays/show.php', array('pays' => $pays,'camps'=>$camps));
            
            $response['html'] = $result->getContent();
            
            return new Response(
                    json_encode(
                                $response
                    ), 200, array('Content-Type' => 'application/json')
            );
        }
        
        
    }
    
     public function ajaxInformationFromCampAction(Request $request)
    {
        $session = $this->getSession();
        
        if($request->isXmlHttpRequest()) 
        {
            
            $form = $request->request->all();
            
            $nomCamp = $form['nomCamp'];
            
            $camps = CampQuery::create()->filterByVille($nomCamp)->findOne();
            
            $response['valid'] = true;
            
            $result= $this->render('camp/show.php', array('camps' => $camps));
            
            $response['html'] = $result->getContent();
            
            return new Response(
                    json_encode(
                                $response
                    ), 200, array('Content-Type' => 'application/json')
            );
        }
        
        
    }
}